/**
 * Created by yuliang on 2015/8/13.
 */

var City;
if (!City)
    City = {};

showTag = function (value, row, index) {
    if (value == '1') return '<span style="text-size:5px;color: red">热门城市</span>';
    if (value == '2') return '<span style="text-size:5px">普通城市</span>';
}

// var openCityClick = function (id, state) {
//     $.post($().AbsUrlParse("/web/city/setOpenCity"), {id: id, state: state}, function (res) {
//         if (res.state && res.res.code == 40000) {
//             hint('操作成功！');
//             var parentId = $("#parentId").val();
//             queryCitys(parentId);
//         } else {
//             if (res.state == false) {
//                 hint(res.msg);
//             } else {
//                 hint(res.res.msg);
//             }
//         }
//     });
// }

operateFormatter = function (value, row, index) {
    if (row.deptPath == 3) {
        return [
            '<a class="edit" href="javascript:void(0)" title="编辑">编辑</a>&nbsp&nbsp&nbsp' +
            '&nbsp;&nbsp;&nbsp;' +
            '<a class="hotCity" href="javascript:void(0)" title="设置/取消热门">设置/取消热门</a>&nbsp&nbsp&nbsp' +
            '&nbsp;&nbsp;&nbsp;' +
            '<a class="remove" href="javascript:void(0)" title="删除城市">删除</a>' +
            '&nbsp;&nbsp;&nbsp;'
        ].join('');
    } else {
        return [
            '<a class="edit" href="javascript:void(0)" title="编辑">编辑</a>&nbsp&nbsp&nbsp' +
            '&nbsp;&nbsp;&nbsp;' +
            '<a class="remove" href="javascript:void(0)" title="删除城市">删除</a>' +
            '&nbsp;&nbsp;&nbsp;'
        ].join('');
    }

}

/**
 * 添加
 */
bindClick("#addCity", function () {
    $("#cityId").val('');
    showAddCity();
});
/**
 * 添加框
 */
var showAddCity = function () {
    showModel("addCity-div", function () {
        var parentId = $("#parentId").val()
        var deptPath = $("#deptPath").val();
        $("#parent").val(parentId);
        $("#dept").val(deptPath);
    });
}
/**
 * 添加成功
 */
bindClick("button[data-event=submit-add]", function () {

    var div = $("#addCity-div");
    var form = $("#cityform");


    if ($("#cityId").val() == "" || $("#cityId").val().trim() == "") {
        hint("添加地区不能为空");
        return false;
    }

    var params = form.formToArray();
    $.post(form.attr("action"), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('添加成功！');
            //关闭model方法
            closeModel('addCity-div');
            $("#btnsearch1").click();

        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

/**
 * 修改框
 * @param row
 */
var showEditCity = function (row) {
    showModel("editCity-div", function () {
        var div = $("#editCity-div");
        $("input[name=cityId]").val(row.id);
        $("input[name=parentId]").val(row.parentId);
        $("input[name=name]").val(row.name);
    });
}
/**
 * 修改成功
 */
bindClick("button[data-event=submit-edit]", function (tag) {
    var form = $("#mergeForm");
    var v = form.valid();

    if ($("#city").val() == "" || $("#city").val().trim() == "") {
        hint("城市名称不能为空");
        return false;
    }

    var params = form.formToArray();
    $.post(form.attr("action"), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            closeModel('editCity-div');
            comment.refresh();
        } else {
            if (res.state == false) {
            } else {
                alert(res.res.msg);
            }
            operateEvents
        }
    });
});


nameFormatter = function (value, row, index) {
    if (row.deptPath == 0) {
        return '<span class="child line">' + value + '</span>';
    } else if (row.deptPath == 1) {
        return '<span class="child line">' + value + '</span>';
    } else if (row.deptPath == 2) {
        return '<span class="child line">' + value + '</span>'
    } else if (row.deptPath == 3) {
        return '<span class="child line">' + value + '</span>'
    } else if (row.deptPath == 4) {
        return '<span >' + value + '</span>'
    }
}

var pathClick = function (id, parentId, name, deptPath) {
    queryCitys(id, deptPath);
    $("#parentId").val(id);
    $("#deptPath").val(deptPath + 1);
    var dp = deptPath + 1;
    showHideColumn(dp);
    $('#path').append('<span class="path" data-id="' + id + '" onclick="clearPathClick(' + id + ',' + parentId + ',' + dp + ')">' + name + '>&nbsp;&nbsp;</span>');
};
var showHideColumn = function (dp) {
    if (dp == 3) {
        $("#table").bootstrapTable("showColumn", "isOpen");
        $("#table").bootstrapTable("showColumn", "isHot");

        $("#showa").show();

    } else {
        $("#table").bootstrapTable("hideColumn", "isOpen");
        $("#table").bootstrapTable("hideColumn", "isHot");
        $("#showa").hide();
    }
}

var queryCitys = function (id, deptPath) {
    if (deptPath != 2) {
        var table = $("#table");
        var url = $().AbsUrlParse('/web/city/query?parentId=' + id);
        table.bootstrapTable("refresh", {url: url});
    } else {
        var table = $("#table");
        var url = $().AbsUrlParse('/web/city/query?parentId=' + id + "&offset=0");
        table.bootstrapTable("refresh", {url: url});
    }
};

var clearPathClick = function (id, parentId, dept) {
    showHideColumn(dept);
    $("#deptPath").val(dept);

    if (parentId == null) {
        $("#parentId").val('');
    }
    queryCitys(id);
    $("#parentId").val(id);
    var spans = $('#path').children("span");
    var isStart = false;
    $.each(spans, function () {
        var s = $(this);
        if (isStart) {
            s.remove();
        } else {
            var mid = s.attr('data-id');
            if (mid == id) {
                isStart = true;
            }
        }
    });
}

window.operateEvents = {
    'click .child': function (e, value, row, index) {
        pathClick(row.id, row.parentId, row.name, row.deptPath);
    },
    'click .edit': function (e, value, row, index) {
        showEditCity(row);
    },
    'click .hotCity': function (e, value, row, index) {
        comment.post("/web/city/addHot", {
            id: row.id,
            isHot: row.isHot,
            parentId: row.parentId
        }, null, function (data) {
            if (data.res.code == 40000) {
                // hint("操作成功");
                comment.refresh();
            } else {
                hint(data.res.msg);
            }
        });
    },
    'click .remove': function (e, value, row, index) {
        comment.showOperationTips("确定删除吗？", function () {
            comment.post("/web/city/delete", {id: row.id}, null, function (data) {
                if (data.res.code == 40000) {
                    // hint("删除成功");
                    comment.refresh();
                } else {
                    hint(data.res.msg);
                }
            });
        });
    }
}


/**
 * 显示排名
 * @param value
 * @param row
 * @param index
 * @returns {string}
 */
sortingFormatter = function (value, row, index) {
    return "<a href='javascript:void(0)' onclick='showSorting(" + row.sorting + "," + row.id + ")'><span>" + row.sorting + "</span></a>";
};

/**
 * 点击排名事件
 * @param orderNum
 * @param id
 */
var showSorting = function (sorting, id) {
    showModel("showOrderNum-div", function () {
        $("#pid").val(id);
        $("#sorting").val(sorting);
    });
}

/**
 * 判断输入是否为数字
 * @param v
 * @returns {boolean}
 */
function isNumber(v) {
    var str = v.val();
    var reg = parseFloat(str);
    var re = /^\+?[0-9][0-9]*$/;
    if (!re.test(str)) {
        return false;
    }
    return !isNaN(reg);
}

/**
 * 修改轮播推荐排名按钮点击事件
 */
bindClick("button[data-event=merge-orderNum]", function () {
    var form = $("#mergeOrderNum");
    if (!form.valid()) {
        return false;
    }
    var sorting = $("#sorting");
    if (!isNumber(sorting)) {
        hint("请填入非负的正整数.");
        return false;
    }
    var params = form.formToArray();
    comment.post(form.attr("action"), params, null, function (data) {
        if (data.res.code == 40000) {
            closeModel("showOrderNum-div");
            var table = $("#table");
            table.bootstrapTable('refresh');
        } else {
            hint(data.res.msg);
        }
    });
});


$(function () {
    $("#showa").hide();
});
